# -*- coding:utf-8 -*-

from flask import Flask
from flask_cors import CORS
from resources import resources_api, resources_blueprints
from models import db
from config import default


def create_app():
    """
    创建app实例对象方法，注册蓝图
    :return:
    """
    flask_app = Flask(__name__)
    # 跨域设置
    CORS(flask_app)
    # 加载默认配置
    flask_app.config.from_pyfile('./config/default.py')
    # 通过本地环境变了加载配置,需要设置本地环境变量：YUANCHACHACONFIG=./config/xxx.py
    flask_app.config.from_envvar('YUANCHACHACONFIG')
    # 初始化数据库ORM映射
    db.init_app(flask_app)

    # 初始化RESTfull映射
    resources_api.init_app(flask_app)
    # 注册蓝图
    flask_app.register_blueprint(resources_blueprints, url_prefix="/api")

    return flask_app


# 创建app实例对象
app = create_app()

if __name__ == '__main__':
    app.run()
